package com.assets.util;

import android.media.MediaPlayer;
import com.assets.R;
import com.assets.newNHF.UhfApplication;

/**
 * @作者(author): WR
 * @创建时间(date): 2021/4/1
 */
public class MediaUtil {
    private volatile static MediaUtil mediaUtil;
    private volatile MediaPlayer player;

    public static MediaUtil getInstance() {
        if (mediaUtil == null) {
            synchronized (MediaUtil.class) {
                if (mediaUtil == null) {
                    mediaUtil = new MediaUtil();
                }
            }
        }
        return mediaUtil;
    }

    public synchronized void play() {
        if (player != null) {
            if (player.isPlaying()) {
                player.stop();
                player = null;
                initPlayer();
            }
        } else {
            initPlayer();
        }
        player.start();
    }

    private void initPlayer() {
        if (player == null) {
            player = MediaPlayer.create(UhfApplication.getInstance(), R.raw.tag_inventoried);
            player.start();
            player.stop();
            try {
                player.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
